import Vue from 'vue'
import Router from 'vue-router'
import Error from '@/components/Error'
import Main from '@/components/Main.vue'
import HelloWorld from '@/components/HelloWorld.vue'
import ProductList from '@/components/ProductList.vue'
import Login from '@/components/Login.vue'
import Register from '@/components/Register.vue'
import ForgotPassword from '@/components/ForgotPassword.vue'
import ProductDetail from '@/components/ProductDetail.vue'
import ProductNew from '@/components/ProductNew.vue'
import Product from '@/components/Product.vue'
import ProductEdit from '@/components/ProductEdit.vue'
import UserDetail from '@/components/UserDetail.vue'
import UserEdit from '@/components/UserEdit.vue'
import ChangePassword from '@/components/ChangePassword.vue'
import Home from '@/components/Home.vue'
import FileEdit from '@/components/FileEdit.vue'
import FileHistory from '@/components/FileHistory.vue'
import FileNew from '@/components/FileNew.vue'
import CourseDescription from '@/components/CourseDescription.vue'
import CourseDescriptionAdd from '@/components/CourseDescriptionAdd.vue'
import CourseDescriptionEdit from '@/components/CourseDescriptionEdit.vue'
import CourseDescriptionDetail from '@/components/CourseDescriptionDetail.vue'

import config from '@/assets/config.js'
import cookie from '@/server/cookie.js'
import common from '@/server/common.js'
import server from '@/server/server.js'

import VueToastr from '@deveodk/vue-toastr'
import '@deveodk/vue-toastr/dist/@deveodk/vue-toastr.css'

import $ from 'jquery'

Vue.use(Router)
Vue.use(VueToastr, {
    defaultPosition: 'toast-top-right',
    defaultType: 'info',
    defaultTimeout: 2000
})


Vue.prototype.config = config

Vue.prototype.function = {
  // 共通方法
  common: common,
  // cookie方法
  cookie: cookie,
  // 请求
  server: server
}

const router = new Router({
  routes: [
  	// 错误页面
  	{
      path: '*',
      name: 'error',
      component: Error
    },
    // 登录页面
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    // 注册页面
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    // 忘记密码
    {
      path: '/forgotPassword',
      name: 'forgotPassword',
      component: ForgotPassword
    },
    // 用户详情
    {
      path: '/userDetail',
      name: 'userDetail',
      component: UserDetail
    },
    // 用户编辑
    {
      path: '/userEdit',
      name: 'userEdit',
      component: UserEdit
    },
    // 修改密码
    {
      path: '/changePassword',
      name: 'changePassword',
      component: ChangePassword
    },
    // 主页
    {
      path: '/',
      name: 'home',
      component: Home
    },
    // 课程描述
    {
      path: '/courseDescription',
      name: 'courseDescription',
      component: CourseDescription
    },
    // 课程描述 新增
    {
      path: '/courseDescriptionAdd',
      name: 'courseDescriptionAdd',
      component: CourseDescriptionAdd
    },
    // 课程描述 编辑
    {
      path: '/courseDescriptionEdit',
      name: 'courseDescriptionEdit',
      component: CourseDescriptionEdit
    },
    // 课程描述 详情
    {
      path: '/courseDescriptionDetail',
      name: 'courseDescriptionDetail',
      component: CourseDescriptionDetail
    },
    // 项目列表
    {
      path: '/productList',
      name: 'productList',
      component: ProductList
    },
    // 新建项目
    {
      path: '/productNew',
      name: 'productNew',
      component: ProductNew
    },

    { path: '/:productId',
      component: Main,
      children: [
        // 项目
        {
          path: '',
          name: 'product',
          component: Product
        },
        // 项目详情
        {
          path: 'productDetail',
          name: 'productDetail',
          component: ProductDetail
        },
        // 项目编辑
        {
          path: 'productEdit',
          name: 'productEdit',
          component: ProductEdit
        },
        // 文件编辑
        {
          path: ':fileId/fileEdit',
          name: 'fileEdit',
          component: FileEdit
        },
        // 文件历史版本
        {
          path: ':fileId/fileHistory',
          name: 'FileHistory',
          component: FileHistory
        },
        // 文件新增
        {
          path: 'fileNew',
          name: 'fileNew',
          component: FileNew
        }
      ]
    }

  ]
})


router.beforeEach((to, from, next) => {
  // 改变背景颜色
  if (to.name == 'login' || to.name == 'register' || to.name == 'forgotPassword' || to.name == 'productList' || to.name == 'productNew' || to.name == 'userDetail' || to.name == 'userEdit' ||　to.name == 'changePassword') {
    $("body").addClass("gray-bg")
  } else {
    $("body").removeClass("gray-bg")
  }

  // 跳转前check是否登陆
  if (to.name == 'home' ||　to.name == 'productList' ||　to.name == 'productNew' ||　to.name == 'product' ||　to.name == 'productDetail' ||　to.name == 'productEdit' || to.name == 'productNew' || to.name == 'userDetail' || to.name == 'userEdit' ||　to.name == 'changePassword') {
    // 检查是否登陆（仅cookie）
    common.checkLogin(function (loginFlag) {
      if (loginFlag) {
        next()
      } else {
        next({ name: 'login', query: { source: to.name } })
      }
    })
  } else {
    next()
  }
})

export default router